﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LGE;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace EggCatcher
{
    class PlayScreen : GameScreen
    {
        // Variables
        public int points = 0;
        int numberOfChickens = 3;
        double sessionTimer = 60;

        // GameObjects
        public Basket basket;
        List<Chicken> chicken;
        public List<Egg> egg;
        public List<Poop> poop;

        // Interface
        Vector2 pointsPos = new Vector2(1100, 640);
        Texture2D backGround;

        // Sounds
        string SFXPickEgg = "Sounds\\PickEgg";
        string SFXPickPoop = "Sounds\\PickPoop";
        string song = "Sounds\\CatchTheEggs";

        public PlayScreen()
        {
        }

        public override void LoadContent()
        {
            poop = new List<Poop>();
            egg = new List<Egg>();
            chicken = new List<Chicken>();
            float chickenOffset = Engine.viewport.Width / numberOfChickens;
            for (int i = 0; i < numberOfChickens; ++i) { chicken.Add(new Chicken(this)); chicken[i].position = new Vector2(i * chickenOffset + chickenOffset / 2, 100); }
            basket = new Basket(this);
            basket.position = new Vector2(700, 640);
            backGround = Engine.Load<Texture2D>("Images\\bg_playscreen");
            Engine.AddSFX(SFXPickEgg);
            Engine.AddSFX(SFXPickPoop);
            Engine.AddSong(song);
            Engine.PlaySong(song);
        }

        public override void Update(GameTime gameTime)
        {
            if (GameOverCheck(gameTime)) return;
            UpdateInput();
            foreach (Poop p in poop) p.Update(gameTime);
            foreach (Egg e in egg) e.Update(gameTime);
            foreach (Chicken c in chicken) c.Update(gameTime);
            basket.Update(gameTime);
        }

        public bool GameOverCheck(GameTime gameTime)
        {
            sessionTimer -= gameTime.ElapsedGameTime.TotalSeconds;
            if (sessionTimer < 0)
            {
                GameScreenManager.ChangeScreen(new ScoreScreen(points));
                Engine.StopSong();
                return true;
            }
            return false;
        }

        public void UpdateInput()
        {
            if (Input.KeyPressed(Keys.M)) MediaPlayer.IsMuted = !MediaPlayer.IsMuted;
            if (Input.KeyDown(Keys.Left)) basket.Move(Direction.Left);
            if (Input.KeyDown(Keys.Right)) basket.Move(Direction.Right);
            if (Input.KeyPressed(Keys.Back)) sessionTimer = 0;
        }

        public void EggHitBasket()
        {
            points++;
            Engine.PlaySFX(SFXPickEgg);
        }

        public void PoopHitBasket()
        {
            points--;
            Engine.PlaySFX(SFXPickPoop);
        }

        public override void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(backGround, Vector2.Zero, Color.White);
            foreach (Poop p in poop) p.Draw(spriteBatch);
            foreach (Egg e in egg) e.Draw(spriteBatch);
            foreach (Chicken c in chicken) c.Draw(spriteBatch);
            basket.Draw(spriteBatch);
            spriteBatch.DrawString(EggCatcher.Font, "Tempo: " + sessionTimer.ToString("0.0") + "s\nPontos: " + points, pointsPos, Color.Black);
        }
    }
}
